﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogic.Interfaces;
using Domain;
using Domain.Entities;

namespace BusinessLogic.Implementations
{
  public   class EFPrepackageRepository :IPrepackageRepository
    {

        private EFDbContext context;

        public EFPrepackageRepository(EFDbContext context)
        {
            this.context = context;
        }


      public IEnumerable<Prepackage> GetPrepackages()
      {
          return context.Prepackages.ToList().OrderBy(i=>i.Weight);
      }

      public Prepackage GetPrepackageById(int id)
      {
          return context.Prepackages.FirstOrDefault(i => i.Id == id);
      }

      public void CreatePrepackage(Prepackage prepackage)
      {
          context.Prepackages.Add(prepackage);
          context.SaveChanges();
      }

      public void SavePrepackage(Prepackage prepackage)
      {
          if (prepackage.Id == 0)
          {
              context.Prepackages.Add(prepackage);
          }
          else
          {
              var p = GetPrepackageById(prepackage.Id);
              p.Weight = prepackage.Weight;
          }
          context.SaveChanges();
      }

      public void DeletePrepackageById(int id)
      {
          context.Prepackages.Remove(GetPrepackageById(id));
          context.SaveChanges();
      }

    }
}
